// src/mixins/mixin.js
export const navigationMixin = {
  data() {
    return {
      constantRoutes: [],
      currentIndex: 0,
      intervalId: ''
    };
  },
  mounted() {
    // 初始化时将所有路由展平
    this.flattenRoutes(JSON.parse(window.sessionStorage.getItem('constantRoutes')));
  },
  methods: {

    // 展平路由，将子路由合并到主路由数组中
    flattenRoutes(routes) {
      routes.forEach(route => {
        if (route.children && route.children.length > 0) {
          route.children.forEach(child => {
            this.constantRoutes.push(child);
          });
        } else {
          this.constantRoutes.push(route);
        }
      });
    },
    // 跳转到下一个路径
    goToNextPage() {
      this.currentIndex++;
      if (this.currentIndex < this.constantRoutes.length) {
        // 获取下一个路径
        const nextRoute = this.constantRoutes[this.currentIndex].path;
        // 跳转到下一个路径
        // 检查是否跳转到相同组件的不同路径
        if (this.$route.path !== nextRoute) {
          this.$router.push(nextRoute);
        }
      } else {
        console.log("已经是最后一个页面了");
        clearInterval(this.intervalId);
      }
    },
    getSetInterval () {
      this.intervalId = setInterval(() => {
        this.goToNextPage();
      },2000)
    },
  }
};
